package com.example.demo.Info;
import com.example.demo.Demo4Application;
import com.example.demo.Mapper.UserMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;

@Data
public class Room {
    private String room_id;
    private user_info userInfo1;
    private user_info userInfo2;
    private int white_userid;
    private int[][] board=new int[15][15];
    private ObjectMapper objectMapper=new ObjectMapper();
    private Websocket_And_Room_Manage websocket_and_room_manage=Demo4Application.context.getBean(Websocket_And_Room_Manage.class);
    public void putchess(int user_id,int row,int col) throws IOException {
        int chess=0;
        if (user_id==userInfo1.getUserid()){
            board[row][col]=user_id;
            chess=user_id;
        }else if (user_id==userInfo2.getUserid()){
            board[row][col]=user_id;
            chess=user_id;
        }
        int winner=win_or_lose(chess,row,col);

//        第一个响应
        Game_Room_Websocket_Response gameRoomWebsocketResponse1=new Game_Room_Websocket_Response();
        gameRoomWebsocketResponse1.setWinner(winner);
        gameRoomWebsocketResponse1.setMessage("putChess");
        gameRoomWebsocketResponse1.setThis_userid(user_id);
        gameRoomWebsocketResponse1.setRow(row);
        gameRoomWebsocketResponse1.setCol(col);

//        第二个响应
        Game_Room_Websocket_Response gameRoomWebsocketResponse2=new Game_Room_Websocket_Response();
        gameRoomWebsocketResponse2.setWinner(winner);
        gameRoomWebsocketResponse2.setMessage("putChess");
        gameRoomWebsocketResponse2.setThis_userid(user_id);
        gameRoomWebsocketResponse2.setRow(row);
        gameRoomWebsocketResponse2.setCol(col);
//        获取两个玩家的session
        WebSocketSession session1=websocket_and_room_manage.game_room_websocket.get(user_id);
        WebSocketSession session2=websocket_and_room_manage.game_room_websocket.get(user_id==userInfo1.getUserid()?userInfo2.getUserid(): userInfo1.getUserid());
//        将响应发送给各个玩家
        session1.sendMessage(new TextMessage(objectMapper.writeValueAsString(gameRoomWebsocketResponse1)));
        session2.sendMessage(new TextMessage(objectMapper.writeValueAsString(gameRoomWebsocketResponse2)));
    }
    public int win_or_lose(int chess,int row,int col){
//        横向五子连珠判定
        {
            int left_limit=col-4;
            if(left_limit<0){
                left_limit=0;
            }
            int right_limit=col+4;
            if (right_limit>14){
                right_limit=14;
            }
            int count=0;
            for (int i=left_limit ; i<=right_limit ; i++ ){
                if (board[row][i]==chess){
                    count++;
                }else if (count>0){
                    count--;
                }
                if (count==5){
                    return chess;
                }
            }
        }
//        竖向五子连珠判定
        {
            int top_limit=row-4;
            if(top_limit<0){
                top_limit=0;
            }
            int bottom_limit=row+4;
            if (bottom_limit>14){
                bottom_limit=14;
            }
            int count=0;
            for (int i=top_limit ; i<=bottom_limit ; i++ ){
                if (board[i][col]==chess){
                    count++;
                }else if (count>0){
                    count--;
                }
                if (count==5){
                    return chess;
                }
            }
        }
//        左上到右下
        {
            int left_top_row=row-4;
            if (left_top_row<0){
                left_top_row=0;
            }
            int left_top_col=col-4;
            if (left_top_col<0){
                left_top_col=0;
            }
            int right_bottom_row=row+4;
            if (right_bottom_row>14){
                right_bottom_row=14;
            }
            int right_bottom_col=col+4;
            if(right_bottom_col>14){
                right_bottom_col=14;
            }
            int count=0;
            for (int i=left_top_row,j=left_top_col;i<=right_bottom_row&&j<=right_bottom_col;i++,j++){
                if (board[i][j]==chess){
                    count++;
                } else if (count>0) {
                    count--;
                }
                if (count==5){
                    return chess;
                }
            }
        }
//        左下到右上
        {
            int left_bottom_row=row-4;
            if (left_bottom_row<0){
                left_bottom_row=0;
            }
            int left_bottom_col=col+4;
            if (left_bottom_col>14){
                left_bottom_col=14;
            }
            int right_top_row=row+4;
            if (right_top_row>14){
                right_top_row=14;
            }
            int right_top_col=col-4;
            if(right_top_col<0){
                right_top_col=0;
            }
            int count=0;
            for (int i=left_bottom_row,j=left_bottom_col;i<=right_top_row&&j>=right_top_col;i++,j--){
                if (board[i][j]==chess){
                    count++;
                } else if (count>0) {
                    count--;
                }
                if (count==5){
                    return chess;
                }
            }
        }
        return 0;
    }
}
